/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;
import Model.EnigmaModel;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author snowangelic
 */
public class ReplayReader extends Thread implements Runnable {
    File replayFile;
    ReplayGameController controller;
    DataInputStream dis;
    long timeStampOffset=0;
    long pauseTriggerOffset=0;
    int gameState=0;

    public ReplayReader(ReplayGameController controller,File replayFile) throws FileNotFoundException{
        this.replayFile=replayFile;
        this.controller=controller;
        FileInputStream fis = new FileInputStream(replayFile);

         // Here BufferedInputStream is added for fast reading.
        BufferedInputStream bis = new BufferedInputStream(fis);
        dis = new DataInputStream(bis);
    }

    public void run(){
        try {
            timeStampOffset=System.currentTimeMillis();
            String buffer = dis.readLine();
            //buffer=dis.readLine();
            buffer=dis.readLine();
            do{
                if(gameState==EnigmaModel.GAME_PLAYING){
                    String[] command=buffer.split("\\|");
                    System.out.println(command[0]);
                    while(Integer.parseInt(command[0]) > (System.currentTimeMillis()-timeStampOffset)){
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(ReplayReader.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    if(command[1].equals("mm"))
                        sendMouseMovedCommand(Integer.parseInt(command[2]),Integer.parseInt(command[3]));
                    else if(command[1].equals("is"))
                        sendScrollInventoryCommand();
                    else if(command[1].equals("aiu"))
                        sendUseActiveItemCommand();
                    else
                        System.out.println("Bad Command");
                    System.out.println("Command executed");

                    buffer=dis.readLine();
                }
            }
            while(buffer !=null);

        } catch (IOException ex) {
            Logger.getLogger(ReplayReader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void gameStateChanged(int newGameState) {
        //Au démarrage
        if(gameState==0 && newGameState==EnigmaModel.GAME_PLAYING)
            this.start();
        else if(newGameState==1)
            timeStampOffset+=System.currentTimeMillis()-pauseTriggerOffset;
        else if(newGameState==EnigmaModel.GAME_PAUSED)
            pauseTriggerOffset=System.currentTimeMillis();
        gameState=newGameState;
    }

    void moveReplayFile(File newFile) {
        EnigmaUtils.FileCopier.copyFile(replayFile,newFile);
    }

    private void sendUseActiveItemCommand() {
        controller.useActiveItem(new UseActiveItemCommand(this));
    }

    private void sendScrollInventoryCommand() {
         controller.scrollInventory(new ScrollInventoryCommand(this));
    }

    private void sendMouseMovedCommand(int x, int y) {
        controller.mouseMoved(new MouseMovedCommand(x,y,this));
    }
}
